<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const username = ref('')
const password = ref('')
const loading = ref(false)
const errorMsg = ref('')

const handleLogin = async () => {
  if (!username.value || !password.value) {
    errorMsg.value = '请输入用户名和密码'
    return
  }
  
  loading.value = true
  try {
    await authStore.login(username.value, password.value)
    router.push('/chat')
  } catch (error) {
    errorMsg.value = error.message || '登录失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h1>多模态内容理解系统</h1>
      <div class="form-group">
        <input
          type="text"
          class="input"
          v-model="username"
          placeholder="用户名"
          @keyup.enter="handleLogin"
        >
      </div>
      <div class="form-group">
        <input
          type="password"
          class="input"
          v-model="password"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
      </div>
      <div class="error-message" v-if="errorMsg">{{ errorMsg }}</div>
      <button 
        class="btn btn-primary login-btn" 
        @click="handleLogin"
        :disabled="loading"
      >
        {{ loading ? '登录中...' : '登录' }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  background: linear-gradient(135deg, var(--background-color) 0%, #ffffff 100%);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}

h1 {
  text-align: center;
  margin-bottom: 32px;
  color: var(--text-color);
  font-size: 24px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
}

.form-group .input {
  width: 100%;
}

.error-message {
  color: #ff3b30;
  font-size: 14px;
  margin-bottom: 16px;
  text-align: center;
}

.login-btn {
  width: 100%;
  margin-top: 8px;
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}
</style> 